package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.DevMaintenancePlanChangeDao;
import com.sugon.entity.DevMaintenancePlanChangeEntity;
import com.sugon.modules.base.model.vo.ProcessProgressConst;
import com.sugon.service.DevMaintenancePlanChangeService;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 设备维护计划变更表Service实现类
 *
 * @author sugon
 * @date 2020-11-18 15:45:01
 */
@Service("devMaintenancePlanChangeService")
public class DevMaintenancePlanChangeServiceImpl implements DevMaintenancePlanChangeService {
    @Autowired
    private DevMaintenancePlanChangeDao devMaintenancePlanChangeDao;

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevMaintenancePlanChangeEntity queryObject(String id) {
        return devMaintenancePlanChangeDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevMaintenancePlanChangeEntity> queryList(Map<String, Object> map) {
        return devMaintenancePlanChangeDao.queryList(map);
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return devMaintenancePlanChangeDao.queryTotal(map);
    }

    @Override
    public DevMaintenancePlanChangeEntity queryUnHandledByTicketId(String ticketId) {
        Map<String, Object> queryParam = MapUtil.newHashMap();
        queryParam.put("ticketId", ticketId);
        queryParam.put("progress", ProcessProgressConst.DEPT_APPROVING);
        List<DevMaintenancePlanChangeEntity> changeList = queryList(queryParam);
        return CollectionUtil.isNotEmpty(changeList) ? changeList.get(0) : null;
    }

    /**
     * save
     *
     * @param devMaintenancePlanChange 实体
     * @return
     */
    @Override
    public int save(DevMaintenancePlanChangeEntity devMaintenancePlanChange) {
        devMaintenancePlanChange.setId(RandomUtil.randomString(32));
        devMaintenancePlanChange.setGmtCreate(DateUtil.date());
        devMaintenancePlanChange.setGmtModified(devMaintenancePlanChange.getGmtCreate());
        if (StrUtil.isEmpty(devMaintenancePlanChange.getApplier())) {
            devMaintenancePlanChange.setApplier(ShiroUtils.getUserId());
        }
        return devMaintenancePlanChangeDao.save(devMaintenancePlanChange);
    }

    /**
     * update
     *
     * @param devMaintenancePlanChange 实体
     * @return
     */
    @Override
    public int update(DevMaintenancePlanChangeEntity devMaintenancePlanChange) {
        devMaintenancePlanChange.setGmtModified(DateUtil.date());
        return devMaintenancePlanChangeDao.update(devMaintenancePlanChange);
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devMaintenancePlanChangeDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devMaintenancePlanChangeDao.deleteBatch(ids);
    }
}
